<?php

namespace app\cps\service;

use app\model\ResponsibilityPerson;
use app\model\Company;
use app\common\Error;
use app\model\OfficialAccounts;
use think\facade\Session;
use app\model\DistributorBackSet;

class PitcherService
{
    private $model;
    private $companyModel;
    private $officialAccountsModel;
    private $distributorBackSetModel;

    public function __construct(ResponsibilityPerson $model, Company $companyModel, OfficialAccounts $officialAccountsModel, DistributorBackSet $distributorBackSetModel)
    {
        $this->model = $model;
        $this->companyModel = $companyModel;
        $this->officialAccountsModel = $officialAccountsModel;
        $this->distributorBackSetModel = $distributorBackSetModel;
    }

    // 添加/编辑投手
    public function editPitcher(array $params, $userInfo)
    {
        if (empty($params['id'])) {
            $data = $this->model->getInfoByWhere(['name' => $params['name']]);

            if ($data) {
                return ['message' => Error::NAME_OF_THE_RESPONSIBLE_PERSON_ALREADY_EXISTS];
            }

            $params['created_at'] = time();
            $params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);

        } else {
            $params['updated_at'] = time();

            $data = $this->model->getInfoById($params['id'], 'password');

            if ($data['password'] != $params['password']) { // 修改过
                $params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
            }
        }

        $params['pid'] = $userInfo['cps_user_id'];
        if (empty($params['company_id'])) {
            $responsibilityPersonData = $this->model->getInfoById($userInfo['cps_user_id'], 'company_id');
            $params['company_id'] = $responsibilityPersonData['company_id'];
        }

        $id = $this->model->edit($params);

        if (!$id) {
            return false;
        }

        return true;
    }

    // 获取投手列表
    public function getPitcherList(array $params, $userInfo)
    {
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 10;

        $pid = $userInfo['cps_user_id'];
        $where[] = ['r.pid', '=', $pid];

        if (!empty($params['name'])) {
            $where[] = ['r.name', 'like', '%' . $params['name'] . '%'];
        }

        $data = $this->model->getPagingDataByWhere($where, $page, $limit, 'r.id, r.name, r.official_account_id,r.is_depart');

        foreach ($data['data'] as $key => $item) {
            $officialAccountId = explode(',', $item['official_account_id']);
            $data['data'][$key]['official_account_id'] = $officialAccountId;
            $officialAccountData = $this->officialAccountsModel->getDataByWhere(['id' => $officialAccountId]);
            $officialAccount = array_column($officialAccountData, 'name');
            $data['data'][$key]['official_account_name'] = implode(',', $officialAccount);
        }
        return $data;
    }

    // 获取投手详情
    public function getPitcherDetail($id)
    {
        $data =  $this->model->getInfoById($id, 'id, name, official_account_id, password, is_depart');
        if (empty($data)) {
            return [];
        }

        $data['official_account_id'] = explode(',', $data['official_account_id']);

        return $data;
    }

    public function setDepartStatus($params)
    {
        if (empty($params['id']) || !isset($params['is_depart'])) return false;

        $params['is_depart'] = $params['is_depart'] ? 1 : 0;
        $params['updated_at'] = time();

        $id = $this->model->edit($params);

        return $id ? true : false;
    }

    // 投手下拉列表
    public function getSelectList($params, $userInfo = [])
    {
        if (!empty($params['token'])) {
            $pData = $this->model->getInfoByWhere(['token' => $params['token']], 'id');
            if (empty($pData)) {
                return ['message' => Error::TOKEN_ERROR];
            }
            $data = $this->model->getDataByWhere(['pid' => $pData['id']], 'id as pitcher_id, name as pitcher_name');
        } else {
            $data = $this->model->getDataByWhere(['pid' => $userInfo['cps_user_id']], 'id as responsibility_person_id, name as responsibility_person_name');
        }
        
        return $data;
    }

    // 回传设置
    public function backSetting($params, $userInfo)
    {
        try {
            $cpsId = $userInfo['cps_user_id'];
            $data = $this->model->getInfoById($cpsId, 'is_distributor, is_branch_office_manage');
            if (empty($data['is_distributor']) && empty($data['is_branch_office_manage'])) {
                return ['message' => Error::NOT_REQUEST_AUTH_MESSAGE];
            }

            $info = $this->distributorBackSetModel->getInfoByWhere(['responsibility_person_id' => $cpsId]);
            if ($info) {
                // 修改
                $params['id'] = $info['id'];
                $params['updated_at'] = time();
            } else {
                // 新增
                $params['responsibility_person_id'] = $cpsId;
                $params['created_at'] = time();
            }

            $id = $this->distributorBackSetModel->edit($params);

            $info = $this->distributorBackSetModel->getInfoByWhere(['id' => $id], '*');
            return $info;
        } catch (\Exception $e) {
            return ['message' => $e->getMessage()];
        }
    }

    // 获取回传设置
    public function getBackSetSetting($params, $userInfo)
    {
        $cpsId = $userInfo['cps_user_id'];
        $info = $this->distributorBackSetModel->getInfoByWhere(['responsibility_person_id' => $cpsId], '*');
        return $info;
    }
}